package com.front.managementfront.agreement.service.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.front.managementfront.agreement.VO.AgreementParameteVo;
import com.front.managementfront.agreement.entity.HktsAgreement;
import com.front.managementfront.agreement.mapper.HktsAgreementMapper;
import com.front.managementfront.agreement.service.IHktsAgreementService;
import com.front.managementfront.dictionary.mapper.HktsDictionaryItemMapper;
import com.front.managementfront.fegin.OrderAppServiceClient;
import com.front.managementfront.utils.CustomPageUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hkts.management.common.utils.CustomPage;
import com.hkts.management.common.utils.Result;
import com.hkts.management.common.utils.ResultUtil;
import com.hkts.management.common.vo.DriverListInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yck
 * @since 2021-08-01
 */
@Service
@Transactional
@Slf4j
public class HktsAgreementServiceImpl extends ServiceImpl<HktsAgreementMapper, HktsAgreement> implements IHktsAgreementService {
    @Autowired
    private HktsDictionaryItemMapper hktsDictionaryItemMapper;

    @Autowired
    private HktsAgreementMapper hktsAgreementMapper;

    @Autowired
    private OrderAppServiceClient orderAppServiceClient;

    @Override
    public CustomPage<HktsAgreement>  selectPage(Page<HktsAgreement> page, HktsAgreement hktsAgreement) {
        PageHelper.startPage((int) page.getCurrent(), (int) page.getSize());
        QueryWrapper<HktsAgreement> query = Wrappers.query();
        QueryWrapper<HktsAgreement> entityWrapper = getEntityWrapper(query, hktsAgreement);
        List<HktsAgreement> hktsAgreements = hktsAgreementMapper.selectList(entityWrapper);
        PageInfo pageInfo = new PageInfo<>(hktsAgreements);
        CustomPage<HktsAgreement> customPage = CustomPageUtils.getCustomPage(pageInfo);
        return customPage;
    }


    @Override
    public List<HktsAgreement> selectList(HktsAgreement hktsAgreement) {
        QueryWrapper<HktsAgreement> query = Wrappers.query();
        QueryWrapper<HktsAgreement> entityWrapper = getEntityWrapper(query, hktsAgreement);
        return hktsAgreementMapper.selectList(entityWrapper);
    }

    /**
     * 公共查询条件
     *
     * @param entityWrapper
     * @return
     */
    public QueryWrapper<HktsAgreement> getEntityWrapper(QueryWrapper<HktsAgreement> entityWrapper, HktsAgreement hktsAgreement) {
        //条件拼接
        if (StringUtils.isNotBlank(hktsAgreement.getAgreementName())) {
            entityWrapper.like(HktsAgreement.AGREEMENT_NAME, hktsAgreement.getAgreementName());
        }
        if (StringUtils.isNotBlank(hktsAgreement.getAgreementContent())) {
            entityWrapper.like(HktsAgreement.AGREEMENT_CONTENT, hktsAgreement.getAgreementContent());
        }

        return entityWrapper;
    }

    @Override
    public Result<String> boundGoodsCategory(String agreementId, String[] itemId) {
        AgreementParameteVo vo=new AgreementParameteVo();
        vo.setAgreementId(agreementId);
        vo.setItemIds(itemId);
        hktsDictionaryItemMapper.updateItemNullByAgreement(agreementId);
        hktsDictionaryItemMapper.updateItemAgreement(vo);
        return ResultUtil.success("绑定成功！") ;
    }

    @Override
    public String hktsAgreementGetById(String id, String contractId) {
        HktsAgreement byId = this.getById(id);
        String agreementContent = byId.getAgreementContent();
        if(StringUtils.isNotBlank(contractId)){
            Result<Map<String, Object>> priceAndCarCode = orderAppServiceClient.getPriceAndCarCode(contractId);
            log.info("运单信息：{}",priceAndCarCode);
            if(priceAndCarCode.getResult().get("price")!=null){
                agreementContent=agreementContent.replace("${price}",priceAndCarCode.getResult().get("price").toString());
            }else{
                agreementContent=agreementContent.replace("${price}","_____");
            }
            if(priceAndCarCode.getResult().get("carCode")!=null){
                agreementContent=agreementContent.replace("${carCode}",priceAndCarCode.getResult().get("carCode").toString());
            }else{
                agreementContent=agreementContent.replace("${carCode}","_____");
            }
        }else{
            agreementContent=agreementContent.replace("${price}","_____");
            agreementContent=agreementContent.replace("${carCode}","_____");
        }
        return agreementContent;
    }

    @Override
    public List<HktsAgreement> agrementList() {
        QueryWrapper<HktsAgreement> query = Wrappers.query();
        query.lambda().eq(HktsAgreement::getIsShow,"1");
        query.orderByAsc("order_field");
        List<HktsAgreement> hktsAgreements = hktsAgreementMapper.selectList(query);
        for(int i=0;i<hktsAgreements.size();i++){
            hktsAgreements.get(i).setAgreementContent(null);
        }
        return hktsAgreements;
    }

    @Override
    public Integer getMaxOrder() {
        Integer maxOrder = hktsAgreementMapper.getMaxOrder();
        return maxOrder+1;
    }
}
